import { defineStore } from 'pinia'
import api from "../api/index"

export const useTokenStore = defineStore('token', {
  state: () => ({
    user: {
      token: "",
      username: ""
    }
  }),
  actions: {
    setToken({ phone, password, verify }: any, success: Function, fail: Function) {
      api.getLogin({
        phone: phone.value,
        password: password.value,
        verify
      }).then(res => {
        if (res.data.status === 200) {
          this.user.token = res.data.token
          this.user.username = res.data.username
          success()
        } else {
          fail(res.data)
        }
      })
    },
    clearToken() {
      this.user.token = ""
      this.user.username = ""
    }
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'user',
        storage: localStorage,
      },
    ],
  }
})
